<?php
/**
 *
 * @author long
 * everyone can use this service, but need register
 *
 */
class SpinController extends Controller
{

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
				'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
		// 				array('allow',  // allow all users to perform 'index' and 'view' actions
		// 						'actions'=>array(),//index
		// 						'users'=>array('*'),
		// 				),
		array('allow', // allow authenticated user to perform 'create' and 'update' actions
						'actions'=>array('index','preview','query'),
						'users'=>array('@'),
		),
		// 				array('allow', // allow admin user to perform 'admin' and 'delete' actions
		// 						'actions'=>array('admin','delete'),
		// 						'users'=>array('admin'),
		// 				),
		array('deny',  // deny all users
						'users'=>array('*'),
		),
		);
	}

	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('spin');
	}

	public function actionPreview()
	{
		$content = $_REQUEST['content'];
		//var_dump($content);
		$spinned = ArticleGenerateHelper::GenerateArticleByTemplate($content);
		//var_dump($spinned);
		ResponseWrapHelper::_sendDirectResponse($spinned);
	}
	/**
	 *
	 */
	public function actionQuery()
	{
		$word = isset($_REQUEST['word']) ? trim($_REQUEST['word']) : null;

		//if word is phrase
		if(strpos($word,' '))
		{
			$ret = PhraseHelper::getSynonymous($word);
		}
		else {
			//get type
			//TagHelper::initalize();
			$wordTypeArr = TagHelper::tag($word);
			$wordType = trim($wordTypeArr[0]['tag']);

			//stem
			//seems stem not useful for me,
			//$stemHelper = new StemHelper2();
			$wordOrginal = StemHelper::Stem($word);//$stemHelper->stem($word);//StemHelper::Stem($word);
			if ($word == $wordOrginal)
			{
				//judge the passed
			}
			$ret = "";
			switch ($wordType)
			{
				case 'DT':
				case 'NN':
				case 'NNP':
				case 'NNPS':
				case 'NNS':
					$ret = NounDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				case 'JJ':
				case 'JJR':
				case 'JJS':
					$ret = AdjDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				case 'PRP':
				case 'PRP$':
				case 'WP':
					$ret = PronDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				case 'RB':
				case 'RBR':
				case 'RBS':
				case 'WRB':
					$ret = AdvDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				case 'MD':
				case 'VB':
				case 'VBD':
				case 'VBG':
				case 'VBN':
				case 'VBP':
				case 'VBZ':
					$ret = VerbDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				case 'CC':
				case 'IN':
					$ret = ConjDictHelper::getSynonymous($wordOrginal, $wordType);
					break;
				default:
					break;
			};
		}
		$resp = '';

		if(empty($ret))
		ResponseWrapHelper::_sendDirectResponse('no synonymous find');
		//get synonymous from dictionary
		$synonymousArr = explode(',',$ret);
		$resp_format= "<li><input type=\"checkbox\" class=\"check-with-label\" value=\"?\">
  <label for=\"check\" class=\"label-for-check\">?</label></li>";


		foreach ($synonymousArr as $syn)
		{
			$tmp = str_replace('?', $syn, $resp_format);
			$resp.=$tmp;
		}

		ResponseWrapHelper::_sendDirectResponse($resp);//CJSON::encode($rows)
	}
	
}